//
//  FDMMasterCollectionViewCellVoice.m
//  Entertainment
//
//  Created by dllo on 16/4/23.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMMasterCollectionViewCellVoice.h"
#import "FDMMasterVoiceModel.h"

@interface FDMMasterCollectionViewCellVoice ()

@property (nonatomic, retain) UIImageView *titleImage;
@property (nonatomic, retain) FDMBaseLabel *titleLabel;
@property (nonatomic, retain) FDMBaseLabel *editorLabel;
@property (nonatomic, retain) FDMBaseLabel *playLabel;
@property (nonatomic, retain) FDMBaseButton *downloadButton;
@property (nonatomic, retain) UIImageView *playImage;

@end
@implementation FDMMasterCollectionViewCellVoice

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createVoiceCell];
    }
    return self;
}

- (void)dealloc {
    [self.titleLabel release];
    [self.titleImage release];
    [self.playImage release];
    [self.editorLabel release];
    [self.downloadButton release];
    [super dealloc];
}

- (void)createVoiceCell {
    self.titleImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.titleImage];
    [self.titleImage release];
    self.playImage = [[UIImageView alloc] init];
    [self.titleImage addSubview:self.playImage];
    [self.playImage release];
    self.titleLabel = [[FDMBaseLabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel release];
    self.editorLabel = [[FDMBaseLabel alloc] init];
    [self.contentView addSubview:self.editorLabel];
    [self.editorLabel release];
    self.playLabel = [[FDMBaseLabel alloc] init];
    [self.contentView addSubview:self.playLabel];
    [self.playLabel release];
    self.downloadButton = [FDMBaseButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.downloadButton];
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.titleImage.layer.cornerRadius = 50;
    self.titleImage.layer.masksToBounds = YES;
    [self.titleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
        make.width.mas_equalTo(@100);
    }];
    
    self.playImage.layer.cornerRadius = 20;
    self.playImage.layer.masksToBounds = YES;
    self.playImage.image = [UIImage imageNamed:@"find_album_play@2x.png"];
    [self.playImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleImage.mas_top).with.offset(30);
        make.left.equalTo(self.titleImage.mas_left).with.offset(30);
        make.bottom.equalTo(self.titleImage.mas_bottom).with.offset(-30);
        make.right.equalTo(self.titleImage.mas_right).with.offset(-30);
    }];
    
    self.titleLabel.numberOfLines = 2;
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(0);
        make.left.equalTo(self.titleImage.mas_right).with.offset(10);
        make.bottom.equalTo(self.contentView.mas_top).with.offset(60);
        make.right.equalTo(self.contentView.mas_right).with.offset(-10);
    }];
    
    self.editorLabel.textColor = [UIColor grayColor];
    [self.editorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.left.equalTo(self.titleImage.mas_right).with.offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-40);
        make.right.equalTo(self.contentView.mas_right).with.offset(-50);
    }];
    self.playLabel.font = [UIFont systemFontOfSize:14];
    self.playLabel.textColor = [UIColor grayColor];
    [self.playLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.editorLabel.mas_bottom);
        make.left.equalTo(self.titleImage.mas_right).with.offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-20);
        make.width.mas_equalTo(@200);
    }];
    [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_bottom).with.offset(-50);
        make.left.equalTo(self.contentView.mas_right).with.offset(-50);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
        make.right.equalTo(self.contentView.mas_right).with.offset(-10);
    }];
    [self.downloadButton addTarget:self action:@selector(downloadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.downloadButton setImage:[UIImage imageNamed:@"me_more_download@2x.png"] forState:UIControlStateNormal];
}

- (void)downloadButtonAction:(FDMBaseButton *)button {
    
}

- (void)setVoiceModel:(FDMMasterVoiceModel *)voiceModel {
    if (_voiceModel != voiceModel) {
        [_voiceModel release];
        _voiceModel = [voiceModel retain];
        [self.titleImage sd_setImageWithURL:[NSURL URLWithString:_voiceModel.coverMiddle] placeholderImage:PLACEHOLDER_IMAGE];
        self.titleLabel.text = _voiceModel.title;
        self.playLabel.text = [NSString stringWithFormat:@"播放次数：%@", _voiceModel.playtimes];
        self.editorLabel.text = _voiceModel.nickname;
    }
}

@end
